﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestSystem.DataModel;
using System.IO;

namespace TestSystem.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
       //     RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
            GetAccounts();
        }

        protected void LoginButton_Click(Object sender, EventArgs e)
        {
            int result = IsUser();

            if (result == 1)
            {
                SetUser(BindingSource.GetInstance().StudentTag);
                Response.Redirect("~/StudentHomePage.aspx");
            }
            else if (result == 2)
            {
                SetUser(BindingSource.GetInstance().TeacherTag);
                Response.Redirect("~/TeacherHomePage.aspx");
            }
            else if (result == 3)
            {
                SetUser(BindingSource.GetInstance().AdminTag);
                Response.Redirect("~/AdminHomePage.aspx");
            }
        }

        private List<DataModel.Account> accounts = new List<DataModel.Account>();

        private void GetAccounts()
        {
            String filePath = BindingSource.GetInstance().UserFilePath;

            int size = 0;

            if (File.Exists(filePath))
            {
                using (StreamReader streamReader = new StreamReader(filePath))
                {
                    String arg = String.Empty;

                    while (true)
                    {
                        arg = streamReader.ReadLine();

                        if (String.IsNullOrEmpty(arg))
                        {
                            break;
                        }

                        String[] args = arg.Split(' ');

                        DataModel.Account account = new DataModel.Account(args[0], args[1], args[2], args[3]);

                        this.accounts.Add(account);
                        size++;
                    }
                }

                BindingSource.GetInstance().TotalAccounts = size;
            }
           
        }

        private void SetUser(String type)
        {
            DataModel.Account account = new DataModel.Account();

            account.UserName = this.LoginUser.UserName;

            account.Password = this.LoginUser.Password;

            account.Type = type;
        }

        private int IsUser()
        {
            foreach (DataModel.Account account in this.accounts)
            {
                if (account.UserName.Equals(this.LoginUser.UserName) 
                    && account.Password.Equals(this.LoginUser.Password)
                    )
                {
                    if (account.Type.Equals(BindingSource.GetInstance().StudentTag))
                    {
                        return 1;
                    }
                    else if (account.Type.Equals(BindingSource.GetInstance().TeacherTag))
                    {
                        return 2;
                    }
                    else
                    {
                        return 3;
                    }
                }
            }

            return -1;
        }
    }
}
